﻿package sms.sms_vs_jok;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;

import com.androidquery.AQuery;
import com.androidquery.util.XmlDom;
import com.baidu.mobstat.StatService;
import com.mobisage.android.agg.view.AdSageLayout;
import com.mobisage.android.agg.view.AdSageListener;
import com.mobisage.android.agg.view.AdSageManager;
import com.mobisage.android.agg.view.AdSageSize;
import sms.sms_vs_jok.R;

 

import android.common;
import android.R.string;
import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.renderscript.Element;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

public class CatList extends Activity implements AdSageListener {

	private final static String ADSAGE_LOG_TAG = "ADF";
	private String tjkey = common.tjkey;
	private String PublishId = common.PublishId;

	private LinearLayout layoutMain = null;
	private AdSageLayout adSageLayout = null;
	public AdSageSize adsize = AdSageSize.AdSageSize_FullScreen;
	public int layerout = R.layout.cat;
	private AQuery aq = new AQuery(this);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(layerout);
		common.addActivity(this);
		layoutMain = ((LinearLayout) this.findViewById(R.id.layoutMain));
		StatService.setSessionTimeOut(120);
		
		adSageLayout = new AdSageLayout(this, PublishId, adsize);

		adSageLayout.setAdListener(this);
		layoutMain.addView(adSageLayout);
		
		AdSageLayout adgoogle = new AdSageLayout(this, PublishId,
				AdSageSize.AdSageSize_468X60);		 
		layoutMain.addView(adgoogle);
		
		InitListView();

	}

	public void InitListView() {

		GridView list = (GridView) findViewById(R.id.sms_listview);
		
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		XmlResourceParser xml = getResources().getXml(R.xml.tag);
		try {
			// 切换到下一个状态，并获得当前状态的类型
			int eventType = xml.next();
			while (true) {
				 
				if (eventType == XmlPullParser.START_DOCUMENT) {
				}
				
				else if (eventType == XmlPullParser.START_TAG) {
					// 将标签名称和当前标签的深度（根节点的 depth 是 1 ，第 2 层节点的 depth 是 2 ，类推）
					switch (xml.getDepth()) {
					case 1:
						break;
					case 2:
						
						Map<String, Object> map = new HashMap<String, Object>();
						String value = xml.getAttributeValue(null, "name");
						String value1 = xml.getAttributeValue(null, "tag");
						map.put("name", value);
						map.put("tag", value1);
						map.put("img", common.GetDrawableByName(value));
						data.add(map);
						break;
					}

					// 获得当前标签的属性个数
					// int count = xml.getAttributeCount();
					// 将所有属性的名称和属性值添加到 StringBuffer 对象中
					// for (int i = 0; i < count; i++) {
					// sb.append(xml.getAttributeName(i)
					// + "xml.getAttributeValue(i)");
					// }
				}
				 
				else if (eventType == XmlPullParser.END_TAG) {
				}
				 
				else if (eventType == XmlPullParser.TEXT) {
				}
				 
				else if (eventType == XmlPullParser.END_DOCUMENT) {
					 
					break;
				}
				 
				eventType = xml.next();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
        
		/*
		List<Map<String, Object>> newdata = new ArrayList<Map<String, Object>>();
		
		for (int i = 0; i < data.size(); i=i+4) 
		{
			Map<String, Object> map = new HashMap<String, Object>();
			for(int j=0;j<4;j++)
			{
			  Map<String, Object> cMap=data.get(i+j);
			  int n=j+1;
			  map.put("name"+n, cMap.get("name"));
			  map.put("tag"+n, cMap.get("tag"));
			  map.put("img"+n, cMap.get("img"));
			}
			newdata.add(map);
		}		
		*/
		
		SimpleAdapter listAdapter = new SimpleAdapter(this, data,
				R.layout.cat_layout, new String[] { "tag","img"},
				new int[] { R.id.cattitle1,R.id.caticon1});

		list.setAdapter(listAdapter);

		list.setOnItemClickListener(new android.CatViewItemClick());

	}

	
	
	
	/*******************************************************************************/

	@Override
	public void onDestroy() {
		super.onDestroy();

		if (adSageLayout != null) {
			adSageLayout.destroy();
		}
	}

	/*******************************************************************************/

	@Override
	public void onResume() {
		super.onResume();
		StatService.onResume(this);
		adSageLayout.resumeAd();
		/* 发送Begin Track */
		AdSageManager.getInstance().pushSystemBeginTrack(this, PublishId);
	}

	@Override
	public void onPause() {
		super.onPause();
		/* 发送End Track */
		StatService.onPause(this);
		adSageLayout.pauseAd();
		AdSageManager.getInstance().pushSystemEndTrack(this, PublishId);
	}

	/*******************************************************************************/

	/* AdSageListener Begin */
	@Override
	public void onReceiveAd() {
		Log.d(ADSAGE_LOG_TAG, "onReceiveAd");
	}

	@Override
	public void onFailedReceiveAd() {
		Log.d(ADSAGE_LOG_TAG, "onFailedReceiveAd");
	}

	@Override
	public void onReceiveFullScreenAd() {
		Log.d(ADSAGE_LOG_TAG, "onReceiveFullScreenAd");
	}

	@Override
	public void onFailedReceiveFullScreenAd() {
		Log.d(ADSAGE_LOG_TAG, "onFailedReceiveFullScreenAd");
	}

	@Override
	public void onCloseFullAd() {
		Log.d(ADSAGE_LOG_TAG, "onCloseFullAd");
	}

	@Override
	public void onPresentScreen() {
		Log.d(ADSAGE_LOG_TAG, "onPresentScreen");
	}

	@Override
	public void onDismissScreen() {
		Log.d(ADSAGE_LOG_TAG, "onDismissScreen");
	}

	@Override
	public void onFullScreenPresentScreen() {
		Log.d(ADSAGE_LOG_TAG, "onFullScreenDismissScreen");
	}

	@Override
	public void onFullScreenDismissScreen() {
		Log.d(ADSAGE_LOG_TAG, "onFullScreenDismissScreen");
	}

	@Override
	public void onClickAd() {
		Log.d(ADSAGE_LOG_TAG, "onClickAd");
	}
	/* AdSageListener End */
	
	  
    public boolean onOptionsItemSelected(MenuItem item) {
		 
		return common.MenuSelectItem(this, item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu= common.CreateMenu(this, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
}